package entity;

import java.util.ArrayList;

import loadsave.LoadSaveable;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveCollectionComposite;
import loadsave.loadsaveobject.LoadSaveIndex;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;

public class Quest implements LoadSaveable {

	private ArrayList<Event> completedEvents;
	
	public Quest() {
		completedEvents = new ArrayList<Event>();
	}
	
	public void completeEvent(Event event) {
		completedEvents.add(event);
	}
	
	public boolean hasCompleted(Event e) {
		return completedEvents.contains(e);
	}

	@Override
	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Quest");
				LoadSaveCollectionComposite lscc = new LoadSaveCollectionComposite("completedEvents", "ArrayList", "Event");
				int index = 0;
				for (Event e : completedEvents) {
					lscc.add(new LoadSaveIndex(++index), e.getMemento().getState());
				}
				toReturn.add(lscc.getName(), lscc);
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				Iterator<LoadSaveObject> i = lso.get("completedEvents").getIterator();
				for (i.init(); i.hasNext(); i.next()) {
					Event e = new Event();
					e.getMemento().restoreState(i.getCurrVal());
				}
			}
			
		};
	}
	

}
